//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public abstract class StringReference : DataSource, INotifyValueChanged
    {
        public abstract string Formula { get; }
        /* private StringData strCache;

        internal StringData StringCache
        {
            get { return strCache; }
            set { strCache = value; }
        }*/


        public override IEnumerable<object> Values
        {
            get { foreach (StringValue o in StringValues) yield return o; }
        }

        public abstract IEnumerable<StringValue> StringValues { get; }

        internal static StringReference ReadStringReference(ReadContext context)
        {
            var factory = ReadContextData.GetReferenceFactory(context);
            StringReference result = factory.CreateStringReference();
            result.ReadXmlCore(context);
            return result;
        }


        protected override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("strRef"))
            {
                c.WriteElementString("f", Formula);
                WriteData(c);
                //\\  c.WriteOptionalElement(strCache);
                c.WriteOptionalElement(Extension);
            }
        }

        protected abstract void WriteData(WriteContext context);

        /*{
            get {
                List<object> result = new List<object>();
                foreach (object o in StringCache.Values)
                    result.Add(o);
                return result;
            }
        }*/
        public event EventHandler ValueChanged;
        protected internal void OnValueChanged()
        {
            if (ValueChanged != null)
                ValueChanged(this, new EventArgs());
            NotifyPropertyChanged("Values");
            NotifyPropertyChanged("StringValues");
        }


    }
}